capture	log close
log using MAR07-CrossModels-UNODC,  replace text

//  program:    Stata 
//  task:       Cross-Sectional Models 
//  project:    Markets  

version
clear all
macro drop _all
set linesize 80
set more off
local tag " 06-25-24| Cleaned 06-25-24"
local file "MAR07-CrossModels-UNODC"
local note "|`tag' | `file'"
local opt "noparen sideway excel noaster  bdec(2)  sdec(2)  pdec(3)   adec(2) e(r2) stats(coef se pval)"
local dv "lrhom_un"
local iv "fraser"
local iv2 "infantmort" 
local cont "edu unemp  popdense perurban  sexratio"

//	#0
//	Loading data 
use MAR06-PanelModels-UNODC.dta, clear 

//	#1
//	Setting up 
gen time=.
replace time=0 if year<2010
replace time=1 if year>=2010

//	#2
//	collapsing time 1
preserve 
drop if time!=0 
collapse (mean) `dv' `iv' heritage rol `iv2' `cont' (firstnm) nation regioncode regionname countrycode region subregion region_un region_un_sub region_wb, by(CID) 
save `file'0.dta, replace 
restore 

preserve 
drop if time!=1
collapse (mean) `dv' `iv' heritage rol `iv2' `cont' (firstnm)  nation regioncode regionname countrycode region subregion region_un region_un_sub region_wb, by(CID) 
save `file'1.dta, replace 
restore 

//	#3
//	Loading Time 0 
use `file'0.dta, clear 

//	#4
//	Regression Time 0 
reg `dv' `iv' `iv2' `cont', beta 
vif 
hettest
outreg2 using `file'0, replace `opt' 

foreach var in heritage rol {
reg `dv' `var' `iv2' `cont', beta 
vif 
hettest
outreg2 using `file'0, append `opt'
}

//	#5
//	Mediating Regression `iv' 
foreach var in fraser heritage rol {
sem ( `iv2'-> `var', ) (`var' `iv2' -> `dv', ) (`cont' -> `dv', ),  vce(cluster CID) nocapslatent
outreg2 using `file'0, append `opt' 
sem ( `var'-> `iv2', ) (`var' `iv2' -> `dv', ) (`cont' -> `dv', ),  vce(cluster CID) nocapslatent
outreg2 using `file'0, append `opt' 
}

//	#6
//	Moderating 
foreach var in fraser heritage rol {
	reg `dv' c.`var'##c.`iv2' `cont', beta
	vif
	hettest
	outreg2 using `file'0, append `opt'
	}

//	#7
//	Qunatile regression 
foreach var in fraser heritage rol {
sqreg `dv' `var' `iv2' `cont' , quantile(.25 .5 .75 .90) reps(100) 
outreg2 using `file'0-quant, append `opt'
}

//	#8
//	Loading second dataset 
use `file'1.dta, clear 

//	#9
//	Regression Time 1 
reg `dv' `iv' `iv2' `cont', beta 
vif 
hettest
outreg2 using `file'1, replace `opt' 

foreach var in heritage rol {
reg `dv' `var' `iv2' `cont', beta 
vif 
hettest
outreg2 using `file'1, append `opt'
}

//	#10
//	Mediating Regression `iv' 
foreach var in fraser heritage rol {
sem ( `iv2'-> `var', ) (`var' `iv2' -> `dv', ) (`cont' -> `dv', ),  vce(cluster CID) nocapslatent
outreg2 using `file'1, append `opt' 
sem ( `var'-> `iv2', ) (`var' `iv2' -> `dv', ) (`cont' -> `dv', ),  vce(cluster CID) nocapslatent
outreg2 using `file'1, append `opt' 
}

//	#11
//	Moderating 
foreach var in fraser heritage rol {
	reg `dv' c.`var'##c.`iv2' `cont', beta
	vif
	hettest
	outreg2 using `file'1, append `opt'
	}

//	#12
//	Qunatile regression 
foreach var in fraser heritage rol {
sqreg `dv' `var' `iv2' `cont' , quantile(.25 .5 .75 .90) reps(100) 
outreg2 using `file'1-quant, append `opt'
}

//	#13
//	save and close 
save `file'.dta, replace 
log close 
//